/**
 *
 */
package com.evil.Servlet;

import com.evil.Utils.Log;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/******************************************
 * 工程名：WebTest<br>
 * 包名：com.evil.base<br>
 * 时间：2016年7月26日 下午2:16:51<br>
 * 作者：白雨濃 evil<br>
 * 邮箱：evilbai@foxmail.com<br>
 * 内容：基础Servlet封装<br>
 ******************************************/
public class BaseServlet extends HttpServlet
{

    protected HttpServletRequest request;
    protected HttpServletResponse response;

    protected PrintWriter out;

    public void doPost() throws IOException
    {

    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        this.request = request;
        this.response = response;
        this.out = response.getWriter();

        doPost();

        out.flush();
        out.close();
    }

    public void doGet() throws IOException
    {

    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        this.request = request;
        this.response = response;
        this.out = response.getWriter();

        doGet();

        out.flush();
        out.close();
    }

    protected void print(String s)
    {
        log(s);
        out.print(s);
    }

    protected void printJSON(Object obj)
    {
        String s = new Gson().toJson(obj);
        log(s);
        out.print(s);
    }

    protected String getParameter(String s)
    {
        return request.getParameter(s);
    }

    protected int getParameterInt(String s)
    {
        try
        {
            return Integer.valueOf(request.getParameter(s));
        }
        catch (Exception e)
        {
            return 0;
        }
    }

    public void log(String msg)
    {
        if (Log.DEBUG)
            System.out.println(msg);
    }

    public void log(Object msg)
    {
        if (Log.DEBUG)
            System.out.println(msg);
    }

}
